import apiClient from './api'

// 工具接口定义
export interface Tool {
  id: string
  name: string
  description: string
  type: 'builtin' | 'custom'
  parameters: Array<{
    name: string
    type: string
    required: boolean
    description: string
    default?: any
  }>
  enabled: boolean
  icon?: string
  category: string
}

// 工具调用请求接口
export interface ToolCallRequest {
  toolId: string
  parameters: Record<string, any>
  sessionId?: string
}

// 工具调用响应接口
export interface ToolCallResponse {
  id: string
  toolId: string
  status: 'pending' | 'running' | 'completed' | 'failed'
  result?: any
  error?: string
  startTime: string
  endTime?: string
}

// 工具服务类
class ToolService {
  // 获取工具列表
  async getTools(params?: {
    enabled?: boolean
    category?: string
    search?: string
  }) {
    const response = await apiClient.get('/v1/tools', { params })
    return response
  }

  // 获取工具详情
  async getToolDetail(toolId: string) {
    const response = await apiClient.get(`/v1/tools/${toolId}`)
    return response
  }

  // 更新工具配置
  async updateTool(toolId: string, data: {
    enabled?: boolean
    parameters?: Record<string, any>
  }) {
    const response = await apiClient.put(`/v1/tools/${toolId}`, data)
    return response.data
  }

  // 调用工具
  async callTool(request: ToolCallRequest): Promise<ToolCallResponse> {
    const response = await apiClient.post('/v1/tools/call', request)
    return response.data
  }

  // 获取工具调用历史
  async getToolCallHistory(params?: {
    toolId?: string
    sessionId?: string
    status?: string
    page?: number
    pageSize?: number
  }) {
    const response = await apiClient.get('/v1/tools/history', { params })
    return response
  }

  // 获取工具调用结果
  async getToolCallResult(callId: string) {
    const response = await apiClient.get(`/v1/tools/call/${callId}`)
    return response
  }

  // 取消工具调用
  async cancelToolCall(callId: string) {
    const response = await apiClient.post(`/v1/tools/call/${callId}/cancel`)
    return response
  }

  // 获取工具分类
  async getToolCategories() {
    const response = await apiClient.get('/v1/tools/categories')
    return response
  }

  // 测试工具连接
  async testToolConnection(toolId: string) {
    const response = await apiClient.post(`/v1/tools/${toolId}/test-connection`)
    return response
  }
}

export default new ToolService()